package jaux.tank.generator.jd.components;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import lombok.NonNull;

public class JdTextField extends JdLabeledInput<String, Object> {

	private static final long serialVersionUID = 1L;
	private JTextField textInput;
	

	public JdTextField(String label, String text) {
		super(label, text);

	}

	@Override
	public JComponent createInputComponent(Object  params) {
		textInput = new JTextField();

		return textInput;
	}

	@Override
	public String getValue() {
		return this.textInput.getText();
	}

	@Override
	public void setValue(String value) {
		this.textInput.setText(value);

	}

	public void setEditable(boolean b) {
		this.textInput.setEditable(b);
	}

	@Override
	protected void registerChangeHandler(Consumer<String> handler) {
		textInput.getDocument().addDocumentListener(new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent e) {
				handler.accept(getValue());
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent e) {
				handler.accept(getValue());
				
			}
			
			@Override
			public void changedUpdate(DocumentEvent e) {
				handler.accept(getValue());
				
			}
		});
		
	}
}
